package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"grpc-common/search/types/search"
	"search/internal/config"
	"search/internal/server"
	"search/internal/svc"
	"search/kafka"
	"search/kafka/consumer"
)

var configFile = flag.String("f", "etc/config.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)
	go ConsumerClient()
	go ConsumerSuggestClient()
	go ConsumerProcessClient(c)
	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		search.RegisterLiveServer(grpcServer, server.NewSearchServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	defer s.Stop()

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}
func ConsumerClient() {
	client := kafka.InitKafkaClient()
	chatConsumer := consumer.NewChAlbumConsumer(client)
	err := chatConsumer.Start()
	if err != nil {
		zap.L().Error("kafka chatConsumer 启动失败", zap.Error(err))
		return
	}
}
func ConsumerSuggestClient() {
	client := kafka.InitKafkaClient()
	suggestConsumer := consumer.NewSuggestConsumer(client)
	err := suggestConsumer.Start()
	if err != nil {
		zap.L().Error("kafka chatConsumer 启动失败", zap.Error(err))
		return
	}
}
func ConsumerProcessClient(config2 config.Config) {
	client := kafka.InitKafkaClient()
	processConsumer := consumer.NewProcessConsumer(config2, client)
	err := processConsumer.Start()
	if err != nil {
		zap.L().Error("kafka chatConsumer 启动失败", zap.Error(err))
		return
	}
}
